<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Setting extends CI_Controller {

	var $per_page;
	var $is_view;
	var $is_edit;

	public function __construct()
	{
		parent::__construct();	

		$logged_in = $this->session->userdata('logged_in');
		if($logged_in)
		{
		   $session_data = $this->session->userdata('logged_in');
		   $data['username'] = $session_data['username'];		   
		}
		else
		{
	     //If no session, redirect to login page
	     redirect('login', 'refresh');
   		}   	

   		// gán giá trị tiêu đề form
		$this->template->set('nav',$this->uri->segment(1));
		$this->template->set('title', 'Web Settings');
		$this->template->set('section', 'Web Settings');	

		// load model kho
		$this->load->model('Storage_model');	
		// load model history kho
		$this->load->model('Storage_product_history_model');
		// load products that in storage
		$this->load->model('Storage_product_model');
		// load product size
		$this->load->model('Product_sizes_model');
		// load product model
		$this->load->model('Product_model');	
		// load slide model
		$this->load->model('Slide_model');
		// load setting model
		$this->load->model('Setting_model');
		// load new model
		$this->load->model('New_model');

		// define per_page
		$this->per_page = 10;	
	}

	public function index()
	{
		$data['setting'] = $this->Setting_model->getData();
		$data['slide'] = $this->Slide_model->getSlide();
		$data['news'] = $this->New_model->infos();		

		$this->template->load('admin/template','admin/setting',$data);
	}

	public function update()
	{
		$is_uploadImage = $this->validate_image();

		if(!$is_uploadImage)
		{
			///////////// error upload //////////////////////////////////////
			$error_upload = $this->session->userdata('error_upload');

			if(!empty($error_upload))
				$data['error_upload'] = $error_upload;
			/////////////////////////////////////////////////////////////////			
		}
		else
		{			
			// resize image 182x250
			if(!empty($_FILES['userfile']['name']))
			{
				$this->resize_image($is_uploadImage, 361, 84);					
			}

			$data = array(
					'web_title'		=>	$this->input->post('web_title'),
					'keyword'		=>	$this->input->post('keyword'),
					'metadata'		=>	$this->input->post('metadata'),
					'logo'			=>	$is_uploadImage,								
				);

			$this->Setting_model->insert($data);
		}

		redirect('setting/index');
	}

	public function slide()
	{		
		$is_uploadImage = $this->validate_image();

		if(!$is_uploadImage)
		{
			///////////// error upload //////////////////////////////////////
			$error_upload = $this->session->userdata('error_upload');

			if(!empty($error_upload))
				$data['error_upload'] = $error_upload;
			/////////////////////////////////////////////////////////////////			
		}
		else
		{			
			// resize image 182x250
			if(!empty($_FILES['userfile']['name']))
			{
				$this->resize_image($is_uploadImage, 2000, 270);					
			}

			$data = array(
					'slide'			=>	$is_uploadImage,								
				);

			$this->Slide_model->insert($data);
		}

		redirect('setting/index');
	}


	//////////////////////////////////////////////////////////////

	public function validate_image()
	{
	    $config['upload_path'] = './uploads/image/slide/';
	    $config['allowed_types'] = 'gif|jpg|png|jpg|jpeg';
	    $config['max_size']    = '10000';
	    $config['max_width']  = '10240';
	    $config['max_height']  = '7680';

	    $this->upload->initialize($config);

	    // if empty image upload then return true, since nothing to update
	    $image_upload = $_FILES['userfile']['name'];

	    if(empty($image_upload))
	    	return true;
	    
	    if (!$this->upload->do_upload('userfile'))
	    {
	        $this->session->set_userdata('error_upload', $this->upload->display_errors());
	        return false;
	    } else {
	    	$data = array('upload_data' => $this->upload->data());
	    	
	        return $data['upload_data']['file_name'];
	    }
	}	

	// resize image
	public function resize_image($uploaded_image,$width,$height)
	{
		$this->load->library('image_lib');

		$config['image_library'] = 'gd2';
		$config['source_image']	= './uploads/image/slide/'.$uploaded_image;
		$config['create_thumb'] = false;
		$config['maintain_ratio'] = false;
		$config['width']	 = $width;
		$config['height']	= $height;

		$this->image_lib->clear();
	    $this->image_lib->initialize($config);
	    $this->image_lib->resize();
	}
}